package core.proto;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import static app.constants.CollabVizConstants.*;

class IconCellRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 1L;

	protected Color m_textSelectionColor;
	protected Color m_textNonSelectionColor;
	protected Color m_bkSelectionColor;
	protected Color m_bkNonSelectionColor;
	protected Color m_borderSelectionColor;

	static final Color nonSearchResultColor = Color.LIGHT_GRAY;

	protected boolean m_selected;

	public IconCellRenderer() {
		super();
		m_textSelectionColor = Color.black;//UIManager.getColor("Tree.selectionForeground");
		m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
		m_bkSelectionColor = new Color(195,217,255);
		/* Color Change
		 Previously

		m_bkSelectionColor = Color.YELLOW; //UIManager.getColor("Tree.selectionBackground");
		 */

		m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
		m_borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor");
		setOpaque(false);
	}

	public Component getTreeCellRendererComponent(JTree tree, 
			Object value, boolean isSelected, boolean expanded, boolean leaf, 
			int row, boolean hasFocus) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;//get the node to be rendered

		Color backgroundColor = m_textSelectionColor;


		// set text
		Object obj = node.getUserObject();// the IconData object repres. the node
		setText(obj.toString());

		setIconAndToolTip( !((IconData) obj).isSearchHit(), node, tree);

		// set icon
		if (obj instanceof IconData) {
			IconData icon = (IconData)obj;//IconData object
			CaseNode casenode = (CaseNode) icon.getObject();
			this.setFont(icon.getFont());
			backgroundColor = icon.m_backgroudColor;

			// set background
			if (icon.m_icon.equals(icon.m_expandedIcon) && node.isLeaf()){
				icon.m_backgroudColor= Color.WHITE;
				//iterate through openTabs to see if the file is one of the opened files
				//if (FileTree.getSearchBar().isSearchStarted()!=true){
				Iterator iterator = AllComponents.openTabs.iterator();
				while (iterator.hasNext()){
					if (casenode.getAbsolutePath().equals(iterator.next())){
						icon.m_backgroudColor = OPEN_FILE_NODE_COLOR; 
						if (casenode.getAbsolutePath().equals(AllComponents.getPreviousPath())) {
							icon.m_backgroudColor = OPEN_FILE_NODE_COLOR;
						}
						if (casenode.getAbsolutePath().equals(AllComponents.getCurrentPath())) {
							icon.m_backgroudColor = OPEN_FILE_NODE_FOCUS_COLOR; 
						}
					}
				}
			}
			/* obsolete, we no longer highliht matching entries. 
			else if (icon.m_icon.equals(icon.m_expandedIcon) && node.isLeaf() && 
			        FileTree.getSearchBar().searchField.getText().length()>0){
                if (node.getUserObject().toString().toLowerCase().indexOf(FileTree.getSearchBar().searchField.getText().toLowerCase()) != -1) {               
                    icon.m_backgroudColor = FileTree.SEARCHED_NODE_BACKGROUND_COLOR;
                } else {
                    icon.m_backgroudColor = Color.WHITE;
                }
			}*/
		} else {
			setIcon(null);
		}
		tree.repaint();
		// set font
		setFont(tree.getFont());

		// set foreground color
		setForeground(isSelected ? m_textSelectionColor : m_textNonSelectionColor);
		if (obj instanceof IconData && ! ((IconData) obj).isSearchHit()) {
			setForeground(nonSearchResultColor);
		}

		// set background color
		if (isSelected) {
			setBackground(m_bkSelectionColor);
		} else {
			setBackground(backgroundColor);			
		}
		m_selected = isSelected;

		return this;
	}

	public void paintComponent(Graphics g) 
	{
		Color bColor = getBackground();
		Icon icon = getIcon();

		g.setColor(bColor);
		int offset = 0;
		if(icon != null && getText() != null) 
			offset = (icon.getIconWidth() + getIconTextGap());
		g.fillRect(offset, 0, getWidth() - 1 - offset,
				getHeight() - 1);

		if (m_selected)  {
			g.setColor(m_borderSelectionColor);
			g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
		}
		super.paintComponent(g);
	}

	private void setIconAndToolTip(boolean grayOut, DefaultMutableTreeNode node, JTree tree) {
		Object obj = node.getUserObject();
		IconData icon = (IconData) obj;
		if (! grayOut)
			setIcon(icon.m_icon);
		else
			setIcon(icon.grayIcon);

		String text = ""; /**"Location: ";*/
		text = node.toString();
		setToolTipText(text);
	}
}